package co.liyao.service.impl;

import co.liyao.mapper.BizArticleMapper;
import co.liyao.model.BizArticle;
import co.liyao.model.vo.BizArticleVo;
import co.liyao.service.BizArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BizArticleServiceImpl implements BizArticleService {

    @Autowired
    private BizArticleMapper mapper;

    @Override
    public BizArticle findBizArticleById(Integer id) {
        return mapper.findBizArticleById(id);
    }

    /**
     * 显示首页数据
     * @param vo
     * @return
     */
    @Override
    public PageInfo<BizArticle> FindBizArticlePage(BizArticleVo vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<BizArticle> biz = mapper.findByBizArticle(vo);

        List<Long> list = new ArrayList<Long>();

        for (BizArticle b: biz) {
            list.add(b.getId());
        }

        if(list.size()==0){
            //不管查没查出来  都要返回 因为PageHelper返回前台的参数要用
            PageInfo list1= new PageInfo<BizArticle>(biz);
            return list1;
        }

        List<BizArticle> EndBiz = mapper.listTagsByArticleId(list);

        // listTag, 重新组装数据为{id: Article}
        Map<Long, BizArticle> tagMap = new LinkedHashMap<>(EndBiz.size());
        for (BizArticle bizArticle : EndBiz) {
            tagMap.put(bizArticle.getId(), bizArticle);
        }
        List<BizArticle> boList = new LinkedList<>();
        for (BizArticle bizArticl : biz) {
            BizArticle tagArticle = tagMap.get(bizArticl.getId());
            bizArticl.setTags(tagArticle.getTags());
            boList.add(tagArticle);
        }
        PageInfo bean = new PageInfo<BizArticle>(biz);
        bean.setList(boList);
        return bean;
    }

    @Override
    public List<BizArticle> findByBizArticleTime() {
        return mapper.findByBizArticleTime();
    }

    @Override
    public List<BizArticle> findRec() {
        return mapper.findRec();
    }

    @Override
    public List<BizArticle> findLook() {
        return mapper.findLook();
    }


}
